#include <iostream>
#include <memory>

class Base
{
    public:
    virtual void foo() = 0;
};

class Derived : public Base
{
    public:
    void foo()
    {
        std::cout << "In Derived " << std::endl;
    }

};

class OtherDerived : public Base{
public:
    void foo()
    {
        std::cout << "In OtherDerived " << std::endl;
    }
};

class Factory{
    public:
    template<typename T>
    static std::unique_ptr<Base> create(){
        // return new T();
        return std::make_unique<T>();
    }
    
};

int main()
{
   std::unique_ptr<Base> pBase = Factory::create<Derived>();
    pBase->foo();
   std::unique_ptr<Base> pBase2 = Factory::create<OtherDerived>();
    pBase2->foo();

    // 用这种方法转, 编译不过
    // std::unique_ptr<Derived> pDerived = std::move(std::dynamic_pointer_cast<Derived>(pBase));
    // pDerived->foo();

}